﻿package {
	import flash.display.*;
	import flash.utils.*;
	import flash.events.*;
	import flash.filters.*;
	public class Main extends Sprite {
		private var stageW:Number;
		private var stageH:Number;
		private var LinearRelation:Number;
		private var HolderItems:Array = new Array();
		private var MoveEasing:int = 25;
		private var Layer:int = 10;
		//构造函数
		public function Main():void {
			this.addEventListener(Event.ADDED_TO_STAGE,addHd);
		}
		private function addHd(e:Event):void {
			Initialize();
		}
		//初始化函数
		private function Initialize():void {
			//设置舞台属性
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			stage.displayState = StageDisplayState.NORMAL;
			//添加事件(舞台大小被改变)
			stage.addEventListener(Event.RESIZE,StageResize);
			resizeBg();
			//动画核心函数
			ItemsHolder();
		}
		//舞台大小被改变时被调用的函数
		private function StageResize(event:Event):void {
			resizeBg();
		}
		private function resizeBg():void {
			stageW = stage.stageWidth;
			stageH = stage.stageHeight;
			BackgroundImage.width = stageW;
			BackgroundImage.height = stageH;
		}
		//动画核心函数
		private function ItemsHolder():void {
			var Items:int = 60;

			var Holder:Sprite = new Sprite();
			

			var Blur:BlurFilter = new BlurFilter();
			//设置模糊滤镜
			Blur.blurX = 10;
			Blur.blurY = 10;
			Blur.quality=BitmapFilterQuality.HIGH;

			addChild(Holder);
			//初始化并添加物件
			for (var i:int = 0; i < Layer; i++) {
				HolderItems[i] = new Sprite();
				Holder.addChild(HolderItems[i]);

				for (var j:int = 0; j<Items; j ++) {
					var TempItem:Icon = new Icon();
					TempItem.x = Math.random()*(i+3)*stageW;
					TempItem.y = Math.random()*(i+3)*stageH;

					if (i<1) {
						//添加模糊滤镜
						TempItem.filters = [Blur];
					} 
					var Random:Number = Math.random();
					TempItem.scaleX = .1+Random;
					TempItem.scaleY = .1+Random;
					TempItem.alpha = Math.random() - .2;

					HolderItems[i].addChild(TempItem);
				}
				j = 0;

			}
			i = 0;
			//
			this.addEventListener(Event.ENTER_FRAME, UpdateMoveRun);
		}
		//物件位置移动的函数
		private function UpdateMoveRun(e:Event):void {
			for (var i:int = 0; i < Layer; i ++) {
				//获取移动基数X
				LinearRelation = (stageW-HolderItems[i].width) / stageW;
				//设置移动X
				HolderItems[i].x -= (HolderItems[i].x - LinearRelation*mouseX) / MoveEasing;
				//获取移动基数Y
				LinearRelation = (stageH-HolderItems[i].height) / stageH;
				//设置移动Y
				HolderItems[i].y -= (HolderItems[i].y - LinearRelation*mouseY) / MoveEasing;
			}
		}
	}
}